/*
目标：获取分类数据存储并渲染
1：创建模块
2：定义state与actions
3:在actions中调用接口获取数据
4：定义数据的类型
5：存储数据
6：在页面导入使用和渲染
*/

import { defineStore } from 'pinia'
import request from '@/utils/request'
import {
  ApiRes,
  CategoryResult,
  CategoryObj,
  CategorySubObj
} from '@/types/data'
const initList = [
  { id: '1005000', name: '居家' },
  { id: '1005002', name: '美食' },
  { id: '1010000', name: '服饰' },
  { id: '1011000', name: '母婴' },
  { id: '1013001', name: '个护' },
  { id: '1019000', name: '严选' },
  { id: '1043000', name: '数码' },
  { id: '109243029', name: '运动' },
  { id: '19999999', name: '杂项' }
]
export default defineStore('category', {
  state () {
    return {
      categoryList: initList as CategoryResult[],
      categoryInfo: {} as CategoryObj,
      categorySubInfo: {} as CategorySubObj // 二级分类数据
    }
  },
  actions: {
    // 首页分类调用api调用
    async homeCategoryHead () {
      const res = await request.request<ApiRes<CategoryResult[]>>({
        url: '/home/category/head'
      })
      this.categoryList = res.data.result
      // console.log(res)
    },
    // 获取一级分类详情
    async category (id: string) {
      const res = await request.request<ApiRes<CategoryObj>>({
        url: '/category',
        params: {
          id: id
        }
      })
      this.categoryInfo = res.data.result
      // console.log('获取一级分类详情', res)
    },
    // 获取二级分类数据
    async categorySubFilter (id: string) {
      const res = await request.request<ApiRes<CategorySubObj>>({
        url: '/category/sub/filter',
        params: {
          id
        }
      })
      this.categorySubInfo = res.data.result
      // console.log('获取二级分类数据', res)
    }
  }
})
